import axios from 'axios'
let base = 'http://www.bestckd.com/lungtest/api'
import router from '../router'

export const getUserInfo = params => { return axios.post(`${base}/user/getUserInfo`, params).then(res => res.data); };

export const action = {
  patient:{
    add:`${base}/patient/createPatient`,
  }
};
let loadingobject="";
// ----------------------------------添加请求拦截器
axios.interceptors.request.use(function (config) {
  // 在发送请求之前做些什么
  //---------------请求
  return config;
}, function (error) {
// 对请求错误做些什么
  //-----------请求失败
  return Promise.reject(error);
});
var _that = this;
// ----------------------------------------------添加响应拦截器
axios.interceptors.response.use(function (response) {
    // 对响应数据做点什么
    if (!response.data.IsSuccess) {
      if(response.statusText=='OK'){
        return response;
      }
      else if(window.vue && window.vue.$message){
        window.vue.$message({
          // message: response.data.ErrorMessage,
          message: "网络异常请稍等哦",
          type: "error"
        });
      }
    }
    else if(response.data.IsSuccess){
      setTimeout(()=>{
        var removePage=document.getElementsByClassName('el-loading-mask');
        if(removePage){
          if(removePage.length>0){
            for(var i=0;i<removePage.length;i++){
              removePage[i].remove();
            }
          }
        }
      },100);

    }
    //---------------请求成功
    return response;
  },  //-----------请求失败
  function (error) {
    router.push({
      path: "/request_Error"
    });
    // 对响应错误做点什么
    return Promise.reject(error);
  });
const post = function(url,params,option){
  var token = "";
  if(JSON.parse(localStorage.getItem('user'))){
    let user = JSON.parse(localStorage.getItem('user'));
    // console.log(user);
    if (user.Token) {
      token = user.Token;
    }
  }
  if(option){
    if(option.ifLoading==true){
      if(option.loadingContent){
        Loading.service({target:option.loadingContent});
      }else{
        Loading.service();
      }
    }
  }else{
  }
  return axios({
    method: 'post',headers: {'Token': token},url: base+url,data: params}).then();
  //return axios.post(base+url, { params: params }).then();
};
const get = function(url,params,option){
  var token = "";
  if(JSON.parse(localStorage.getItem('user'))){
    let user = JSON.parse(localStorage.getItem('user'));
    // console.log(user);
    if (user.Token) {
      token = user.Token;
    }
  }
  if(option){
    if(option.ifLoading==true){
      if(option.loadingContent){
        Loading.service({target:option.loadingContent});
      }
    }
  }
  var index = 0;
  var longUrl = base+url;
  for(var item in params){
    if(index==0){
      longUrl = longUrl + "?"+item+"="+params[item]
    }
    if(index!=0){
      longUrl = longUrl + "&"+item+"="+params[item]
    }
    index++;
  }

  return axios({
    method: 'get',headers: {'Token': token},url: longUrl}).then();
};

//图片上传
export const uploadReportImage = params => { return post(`/remote/upload`, params); };
//api/user/updateDoctorProfile
export const updateDoctorProfile = params => { return post(`/user/updateDoctorProfile`, params); };
//getUserList
export const getUserList = params => { return post(`/user/getUserList`, params); };
//admin/getArticleList
export const getArticleList = params => { return post(`/admin/getArticleList`, params); };
//上传病历信息
export const updateCase=params=>{return post('/user/updateCase',params)};
export const createCase=params=>{return post('/user/createCase',params)};
export const createCaseRecord=params=>{return post('/user/createCaseRecord',params)};
export const updateCaseRecord=params=>{return post('/user/updateCaseRecord',params)};
//获取病历信息
export const getCaseRecordList=params=>{return get('/user/getCaseRecordList/'+params)};
//收藏文章
export const collectArticle=params=>{return post('/admin/collectArticle',params)};
//收藏文章列表
export const getUserArticle=params=>{return get('/admin/getUserArticle/'+params)};
//医生绑定患者user/bindingPatient
export const bindingPatient=params=>{return post('/user/bindingPatient',params)};
//病历列表user/getCaseList
export const getCaseList=params=>{return get('/user/getCaseList/'+params)};
export const getCaseShowInfoByCaseId=params=>{return get('/user/getCaseShowInfoByCaseId/'+params)};
export const updatePatientProfile=params=>{return post('/user/updatePatientProfile',params)};
//所属医生user/getPatientDoctorInfo
export const getPatientDoctorInfo=params=>{return get('/user/getPatientDoctorInfo/'+params)};
//admin/CancelCollectArticle取消收藏
export const CancelCollectArticle=params=>{return post('/admin/CancelCollectArticle',params)};
